package com.zzl.study.demo.tool;

import cn.hutool.core.text.UnicodeUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzl.study.demo.domain.TransResponse;
import com.zzl.study.demo.domain.TransResult;
import com.zzl.study.demo.utils.HttpUtil;
import com.zzl.study.demo.utils.MD5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 百度翻译API服务
 *
 * @author: zhangzl
 * @date: 2025/10/22 15:43
 * @version: 1.0
 */
@Slf4j
public class BaiduTranslationService {

    /**
     * 百度翻译API接口地址
     */
    private static final String TRANS_API_HOST = "https://fanyi-api.baidu.com/api/trans/vip/translate";

    /**
     * 百度翻译app-id
     */
    @Value("${baidu.translation.app-id}")
    private String appId;

    /**
     * 百度翻译密钥
     */
    @Value("${baidu.translation.secret-key}")
    private String secretKey;

    /**
     * JSON对象映射器
     */
    private final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 获取翻译结果
     *
     * @param query 查询内容
     * @param from  源语言
     * @param to    目标语言
     * @return 翻译结果
     */
    @Tool(name = "get_trans_result", description = "获取翻译结果")
    public String getTransResult(@ToolParam(description = "查询内容") String query, @ToolParam(description = "源语言") String from, @ToolParam(description = "目标语言") String to) {
        Map<String, String> params = buildParams(query, from, to);
        try {
            String response = HttpUtil.get(TRANS_API_HOST, params);
            TransResponse transResponse = this.objectMapper.readValue(response, TransResponse.class);
            List<TransResult> transResult = transResponse.getTransResult();
            StringBuilder sb = new StringBuilder();
            sb.append("您的翻译结果如下：\n");
            for (int count = 0; count < transResult.size(); count++) {
                TransResult result = transResult.get(count);
                sb.append(count + 1).append("、【").append(result.getSrc()).append("】").append("翻译为：").append(UnicodeUtil.toString(result.getDst())).append(" \n");
            }
            return sb.toString();
        } catch (Exception e) {
            log.error("获取翻译结果失败", e);
            return "获取翻译结果失败，请稍后再试!";
        }
    }

    /**
     * 构建参数
     *
     * @param query 查询内容
     * @param from  源语言
     * @param to    目标语言
     * @return 参数
     */
    private Map<String, String> buildParams(String query, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);
        params.put("appid", this.appId);
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);
        String src = this.appId + query + salt + this.secretKey;
        params.put("sign", MD5.md5(src));
        return params;
    }

}
